package recursion;

public class ArrayRowColZero73 {
    public static void setZeroes(int[][] matrix) {
        int nRow = matrix.length;
        int nCol = matrix[0].length;
        for(int i = 0; i < nRow;i++){
            for(int j = 0; j < nCol;j++)
                if(matrix[i][j] == 0)
                    setZeroRecur(matrix,i,j,i,j);
        }
    }
    public static void setZeroRecur(int[][] matrix,int oriRow, int oriCol,int row, int col){
        //we need to define the boundary of recursion carefully
        // this time it only affects cells in the same row and col
        if(row >= matrix.length || col >= matrix[0].length || col < 0||row <0 ||(row != oriRow||col == oriCol)||(row == oriRow|| col != oriCol))
            return;
        matrix[row][col] = 0;
        setZeroRecur(matrix,row, col,row + 1,col);
        setZeroRecur(matrix,row, col,row - 1,col);
        setZeroRecur(matrix,row, col,row,col + 1);
        setZeroRecur(matrix,row, col,row,col - 1);
    }

    public static void main(String[] args) {
        int[][] matrix = new int[][]{{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        setZeroes(matrix);
        System.out.println();
    }
}
